package ld.proxy.dyna;

import ld.proxy.UseDao;
import ld.proxy.impl.UseDaoImpl;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @Author: Liudao
 * @Date: 2022-08-08-16:30
 * @Description:
 */
public class UseDaoCGLIBDynaImpl implements MethodInterceptor {

    //创建代理对象
    public UseDaoImpl creatproxy(){
        //定义Enhancer对象
        Enhancer enhancer = new Enhancer();
        //定义父类对象
        enhancer.setSuperclass(UseDaoImpl.class);
        //指定Enhancer的回调
        enhancer.setCallback(this);
        //创建代理对象
        UseDaoImpl o = (UseDaoImpl)enhancer.create();
        return o;

    }

    /**
     * 
     * @param o  proxy
     * @param method
     * @param objects   args
     * @param methodProxy
     * @return
     * @throws Throwable
     */
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
       if (method.getName().equals("query")){
           check1();
       }
       
       
       
       
        return methodProxy.invokeSuper(o,objects);
    }

    private void check1() {
        System.out.println("anquanjiancha");
    }
}
